package org.homeworkteam.Service.ServiceImpl;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.homeworkteam.Service.AiService;
import org.homeworkteam.utils.AiUtils;
import org.springframework.stereotype.Service;


import java.io.IOException;

@Service
public class AiServiceImpl implements AiService {

    private String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/image-understanding/";
    private final String ReviewUrlTop = "https://aip.baidubce.com/rest/2.0/solution/v1/";
    private final String ReviewUrlBottom = "/v2/user_defined?access_token=";

    @Override
    public String getTaskId(String filePath,String question) throws IOException {
//        获取图片base64编码
        String imageBase64 = AiUtils.encodeFileToBase64(filePath, false);

        String requestBody = "{\"image\":\"" + imageBase64 + "\",\"question\":\""+question+"\"}";
        String taskId;
//        将requestBody提交请求 获取taskid
        String responseBody = AiUtils.sendPostRequest(url + "request?access_token=", requestBody);
        System.out.println("TaskIdBody: " + responseBody);
        ObjectMapper mapper = new ObjectMapper();
        try {
//                在返回的result中提取出  task_id   供后续做获取结果使用
            JsonNode rootNode = mapper.readTree(responseBody);
            taskId = rootNode.path("result").path("task_id").asText();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return taskId;
    }

    @Override
    public String getRecognitionResult(String taskId) throws IOException {
//        提交taskid 获取结果
        String requestBody = "{\"task_id\":\"" + taskId + "\"}";
        String responseBody = AiUtils.sendPostRequest(url + "get-result?access_token=", requestBody);
        System.out.println("responseBody:" + responseBody);

        String result;
        ObjectMapper mapper = new ObjectMapper();
        try {
            JsonNode rootNode = mapper.readTree(responseBody);
//            不同的 retCode对应不同内容   1为等待 需要重新调用方法获取结果  0为成功  其他则需要查看API文档
            String retCode = rootNode.path("result").path("ret_code").asText();

            int count = 1;
//            等待中
            while (retCode.equals("1")){
                System.out.println("第"+(count++)+"次调试");
//                    30秒访问一次 获取结果
                Thread.sleep(30 * 1000);
//                获取结果
                responseBody = AiUtils.sendPostRequest(url + "get-result?access_token=", requestBody);
//                json
                rootNode = mapper.readTree(responseBody);
//                分割 从result中获取ret_code
                retCode = rootNode.path("result").path("ret_code").asText();
            }

            System.out.println("responseBody:" + responseBody);

//            如果是错误  则输出错误编码
            String errorMsg = rootNode.path("error_msg").asText();
            String errorCode = rootNode.path("error_code").asText();
            if(!errorCode.isEmpty()){
                result = errorCode;
                System.out.println("error_msg:"+errorMsg);
            }else{
                //            将获取的结果 提取描述 描述就是ai给出的答案与解析
                result = rootNode.path("result").path("description").asText();
                System.out.println(result);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

//    文本审核
    @Override
    public String getTextResult(String content) throws IOException {
        String text = "text="+content;
        return  AiUtils.sendReviewPost(ReviewUrlTop,ReviewUrlBottom,"text_censor", text);
    }

//    图片审核
    @Override
    public String getImageResult(String content) throws IOException {
        String imageBase64 = AiUtils.encodeFileToBase64(content, true);
        String image = "image=" + imageBase64;
        String responseBody = AiUtils.sendReviewPost(ReviewUrlTop,ReviewUrlBottom,"img_censor", image);
        System.out.println("Service:"+responseBody);
        return responseBody;
    }


}
